package com.ioi.dao.impl;

import com.ioi.core.BaseDao;
import com.ioi.dao.BookDao;
import com.ioi.entity.Book;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

/**
 * BookDaoImpl 图书DAO实现类
 *
 * @author LILIBO
 * @since 2024/4/27
 */
public class BookDaoImpl extends BaseDao implements BookDao {

    private static final Logger log = LogManager.getLogger(BookDaoImpl.class);

    /**
     * 添加图书
     *
     * @param book
     * @return
     */
    @Override
    public int add(Book book) {
        String sql = "insert book values(default,?,?,?,?,?,?)";
        return super.executeUpdate(sql, book.getPubId(), book.getName(), book.getAuthor(), book.getPubDate(), book.getNumber(), book.getPrice());
    }

    /**
     * 修改图书
     *
     * @param book
     * @return
     */
    @Override
    public int upd(Book book) {
        String sql = "update book set name=?,author=?,image=?,pubDate=?,number=?,price=? where id=?";
        return super.executeUpdate(sql, book.getName(), book.getAuthor(), book.getImage(), book.getPubDate(), book.getNumber(), book.getPrice(), book.getId());
    }

    /**
     * 删除图书
     *
     * @param id
     * @return
     */
    @Override
    public int del(int id) {
        String sql = "delete from book where id=?";
        return super.executeUpdate(sql, id);
    }

    /**
     * 查询列表
     *
     * @return
     */
    @Override
    public List<Book> list() {
        String sql = "select b.*,p.name pubName from book b left join publisher p on b.pubId=p.id";
        return super.executeQuery(Book.class, sql);
    }

    /**
     * 查询数据量
     *
     * @return
     */
    @Override
    public int count() {
        String sql = "select count(1) from book b left join publisher p on b.pubId=p.id";
        return super.executeCount(sql);
    }

    /**
     * 查询列表
     *
     * @param pageIndex 分页起始位置
     * @param pageSize 页大小
     * @return
     */
    @Override
    public List<Book> page(int pageIndex, int pageSize) {
        String sql = "select b.*,p.name pubName from book b left join publisher p on b.pubId=p.id limit ?,?";
        return super.executeQuery(Book.class, sql, pageIndex, pageSize);
    }

}
